package com.definity.toolkit.ui.widget;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;
import javax.swing.border.Border;

import com.definity.toolkit.ui.Body;
import com.definity.toolkit.ui.Position;
import com.definity.toolkit.ui.widget.paint.Paint;

public class Panel extends JPanel implements Widget<Panel>, WidgetPaint<Panel> {

	private static final long serialVersionUID = -4920941657852896788L;
	private Paint paint;

	public Panel() {

	}
	
	@Override
	public String id() {
		return getName();
	}

	@Override
	public Panel id(String id) {
		setName(id);
		return this;
	}

	@Override
	public Panel properties(Body<Panel> body) {
		body.content(this);
		return this;
	}

	@Override
	public Panel size(int width, int height) {
		setPreferredSize(new Dimension(width, height));
		return this;
	}
	
	public Panel layout(Position position) {
		switch (position) {
		case LEFT:
			setLayout(new FlowLayout(FlowLayout.LEFT));
			break;
		case CENTER:
			setLayout(new FlowLayout(FlowLayout.CENTER));
			break;
		case RIGHT:
			setLayout(new FlowLayout(FlowLayout.RIGHT));
			break;
		case TOP:
		case BOTTOM:
			throw new IllegalArgumentException("Position TOP and BOTTOM are illegal to LinePanel");
		}
		return this;
	}

	@Override
	public Color background() {
		return getBackground();
	}

	@Override
	public Panel background(Color background) {
		setBackground(background);
		return this;
	}

	@Override
	public Color foreground() {
		return getForeground();
	}

	@Override
	public Panel foreground(Color foreground) {
		setForeground(foreground);
		return this;
	}

	@Override
	public Font font() {
		return getFont();
	}

	@Override
	public Panel font(Font font) {
		setFont(font);
		return this;
	}
	
	public Panel opaque(boolean opaque) {
		setOpaque(opaque);
		return this;
	}

	public Panel add(Component c) {
		super.add(c);
		return this;
	}

	public Border border() {
		return getBorder();
	}
	
	public Panel border(Border border) {
		setBorder(border);
		return this;
	}
	
	public Panel paint(Paint paint) {
		this.paint = paint;
		return this;
	}

	@Override
	protected void paintComponent(Graphics g) {
		if (paint != null)
			paint.paint((Graphics2D) g, this);
		super.paintComponent(g);
	}
	
}
